
import GameCtr from "../../Controller/GameCtr";
import { Game } from "./Game";
import GameData from "../../Common/GameData";

const { ccclass, property } = cc._decorator;

@ccclass
export default class JackCube extends cc.Component {

    @property([cc.Sprite])
    spr: cc.Sprite[] = [];

    @property([cc.SpriteFrame])
    arrSprJack: cc.SpriteFrame[] = [];

    arrSprcolor = [ "#327101","#27BB33", "#e2821d", "#a1db3f", "#e4546e", "#e1ac10", "#99bf0c", "#e1d565", "#dbc62f", "#3bcad8",];

    gameRoom: Game = null;
    CubeSprid = [];
    CubeId = -1;
    isfly = false;
    isTouch = false;
    moveNd: cc.Node = null;

    onLoad() {
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this, true);
        for (let index = 0; index < 6; index++) {
            let element = this.node.children[index];
            element.getComponent(cc.Sprite).spriteFrame = null;
        }
    }

    start() {

    }

    /** 设置当前游戏房间 */
    public setGameRoom(room) {
        this.gameRoom = room;
    }

    private onTouchEnd(event) {
        // if (this.gameRoom && this.isTouch && !this.isfly) {
        this.touchEnd(event.touch);
        this.isTouch = false;
        //  }
    }


    //移动水果片
    private touchEnd(touch) {
        this.gameRoom.closeteacting();
        this.gameRoom.Jackcubeid(this.CubeId);
        this.node.getComponent(cc.Animation).play("bigsmall");
    }

    //放上去的时候
    private betJudge() {
        // let canvas = cc.director.getScene().getChildByName("Canvas");
        // if (!this.moveNd.parent) {
        //     return;
        // }
        // let wPos = this.moveNd.convertToWorldSpaceAR(this.moveNd.children[0].getPosition());
        // console.log("betJudge", wPos);
        // let betSuc = false;
        // if (wPos) {
        //     betSuc = this.gameRoom.FkxxlbetJudge(wPos, this.selectCubeId);
        // }
        // if (betSuc) {
        //     this.moveNd.active = false;
        //     delete this.moveNd;
        //     this.isDraged = false;
        //     this.gameRoom.removeRing(this.selectCubeId);
        // } else {
        //     this.GotoOrginpos();
        // }
    }

    // GotoOrginpos() {
    //     this.isfly = true;
    //     let canvas = cc.director.getScene().getChildByName("Canvas");
    //     if (this.originPos) {
    //         let oPos = canvas.convertToNodeSpaceAR(this.node.parent.convertToWorldSpaceAR(this.originPos));
    //         this.moveNd.runAction(cc.sequence(
    //             cc.moveTo(0.2, oPos),
    //             cc.callFunc(function () {
    //                 this.moveNd.active = false;
    //                 delete this.moveNd;
    //                 this.isfly = false;
    //                 this.node.opacity = 255;
    //             }.bind(this))
    //         ));
    //     }
    // }

    onTouchStart() {

    }

    addCube(outCubeSprid) {
        for (let index = 0; index < outCubeSprid.length; index++) {
            let element = outCubeSprid[index];
            this.CubeSprid.push(element);
        }

        this.setCube();
    }

    isMax() {
        if (this.CubeSprid.length >= 6) {
            return true;
        }
        return false;
    }
    clear() {
        this.CubeSprid = [];
        this.setCube();
    }

    resetCube() {
        this.CubeSprid = [];
        this.setCube();
    }

    playAm() {
        console.log('播放特效')
        let ParticleSystem = this.node.getChildByName("Am").getChildByName("Particle").getComponent(cc.ParticleSystem);
        let co = cc.hexToColor(this.arrSprcolor[GameData.level % 10]);
        ParticleSystem.startColor = co;
        ParticleSystem.resetSystem();
    }

    setCube() {
        for (let index = 0; index < 6; index++) {
            let element = this.node.children[index];
            element.getComponent(cc.Sprite).spriteFrame = null;
        }
        for (let index = 0; index < this.CubeSprid.length; index++) {
            let id = this.CubeSprid[index];
            let element = this.node.children[id];

            element.getComponent(cc.Sprite).spriteFrame = this.arrSprJack[GameData.level % 10];
        }
    }

    update(dt) {

    }
}
